home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / binaries / Windows / jsdk / src / sun / servlet / http / HttpServerHandler.java < prev   
Encoding:
Java Source  |  1997-07-18  |  5.4 KB  |  229 lines

  1. /*
  2.  * @(#)HttpServerHandler.java    1.20 97/05/22
  3.  * 
  4.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.0
  20.  */
  21.  
  22. package sun.servlet.http;
  23.  
  24. import javax.servlet.*;
  25. import java.net.*;
  26. import java.io.*;
  27. import sun.servlet.ServletConnection;
  28.  
  29. /**
  30.  * This class represents a connection handler in the servlet server.
  31.  *
  32.  * @version    1.20, 05/22/97
  33.  * @author    David Connelly
  34.  */
  35. public
  36. class HttpServerHandler implements Runnable, ServletConnection {
  37.     /**
  38.      * The server for this handler.
  39.      */
  40.     protected HttpServer server;
  41.  
  42.     /**
  43.      * The servlet request.
  44.      */
  45.     protected final HttpRequest req = new HttpRequest();
  46.  
  47.     /**
  48.      * The servlet response.
  49.      */
  50.     protected final HttpResponse res = new HttpResponse();
  51.  
  52.     /**
  53.      * The current socket connection.
  54.      */
  55.     protected Socket socket;
  56.  
  57.     /**
  58.      * Temporary buffer for file requests.
  59.      */
  60.     protected byte[] buf = new byte[512];
  61.  
  62.     /**
  63.      * The URL prefix for invoking servlets.
  64.      */
  65.     protected static String PREFIX = "/servlet/";
  66.  
  67.     /**
  68.      * The servlet URL prefix length.
  69.      */
  70.     protected static int PREFIX_LEN = PREFIX.length();
  71.  
  72.     /**
  73.      * Creates a new handler for the specified server.
  74.      */
  75.     protected HttpServerHandler(HttpServer server) {
  76.     this.server = server;
  77.     }
  78.  
  79.     /**
  80.      * Runs the connection handler.
  81.      */
  82.     public void run() {
  83.     Socket s;
  84.     while ((s = (Socket)server.getConnection()) != null) {
  85.         try {
  86.         socket = s;
  87.         handleConnection(s);
  88.         s.close();
  89.         } catch (IOException e) {
  90.         e.printStackTrace();
  91.         }
  92.     }
  93.     }
  94.  
  95.     /**
  96.      * Handles a single connection from the client.
  97.      * @param s the connection socket
  98.      */
  99.     protected void handleConnection(Socket s) throws IOException {
  100.     req.init(this);
  101.     res.init(this);
  102.     while (req.next()) {
  103.         res.next();
  104.         if (req.isFullRequest()) {
  105.         res.setProtocol("HTTP/1.0");
  106.         }
  107.         res.setHeader("Server", server.name);
  108.         res.setKeepAlive(req.getKeepAlive());
  109.         try {
  110.         sendResponse(req, res);
  111.         } catch (Exception e) {
  112.         if (res.getTotalBytes() == 0) {
  113.             try {
  114.             res.sendError(res.SC_INTERNAL_SERVER_ERROR);
  115.             } catch (IOException ee) {
  116.             // eat it!
  117.             }
  118.         }
  119.         e.printStackTrace();
  120.         }
  121.         req.finish();
  122.         res.finish();
  123.         if (!res.getKeepAlive()) {
  124.         break;
  125.         }
  126.     }
  127.     }
  128.  
  129.     /**
  130.      * Sends a response to the client.
  131.      * @exception ServletException if thrown by the servlet
  132.      */
  133.     protected void sendResponse(HttpRequest req, HttpResponse res)
  134.     throws ServletException, IOException
  135.     {
  136.     MessageBytes path = req.getRequestPath();
  137.     if (path.startsWith(PREFIX)) {
  138.         String name = parsePath(path, req);
  139.         if (name != null) {
  140.         // invoke servlet
  141.         Servlet s = server.getServlet(name);
  142.         if (s != null) {
  143.             s.service(req, res);
  144.             return;
  145.         }
  146.         System.err.println("Servlet not found: " + name);
  147.         }
  148.     } else {
  149.         String m = req.getMethod();
  150.         if (m.equals("GET") || m.equals("HEAD")) {
  151.         res.sendError(res.SC_FORBIDDEN,"Will not serve files, "+
  152.                            "only servlets");
  153.         return;
  154.         }
  155.     }
  156.     res.sendError(res.SC_NOT_FOUND);
  157.     }
  158.  
  159.     /**
  160.      * Parses a servlet path. Returns the name of the servlet or null if
  161.      * the path was invalid.
  162.      */
  163.     protected String parsePath(MessageBytes path, HttpRequest req) {
  164.     byte[] b = path.getBytes();
  165.     int off = path.getOffset() + PREFIX_LEN;
  166.     int len = path.getLength() - PREFIX_LEN;
  167.     int pos;
  168.     for (pos = off; pos < off + len && b[pos] != '/'; pos++) ;
  169.     if (pos < off + len) {
  170.         req.setPathInfo(b, pos, off + len - pos);
  171.     } else {
  172.         pos = off + len;
  173.     }
  174.     String name = new String(b, 0, off, pos - off);
  175.     off -= PREFIX_LEN;
  176.     req.setServletPath(b, off, pos - off);
  177.     return name;
  178.     }
  179.  
  180.     /**
  181.      * Returns the host name of the server.
  182.      */
  183.     public String getServerName() {
  184.     return server.host;
  185.     }
  186.  
  187.     /**
  188.      * Returns the post number of the server.
  189.      */
  190.     public int getServerPort() {
  191.     return server.port;
  192.     }
  193.  
  194.     /**
  195.      * Returns the local port of the socket connection.
  196.      */
  197.     public String getRemoteHost() {
  198.     return socket.getInetAddress().getHostName();
  199.     }
  200.  
  201.     /**
  202.      * Returns the remote address of the socket connection.
  203.      */
  204.     public String getRemoteAddr() {
  205.     return socket.getInetAddress().getHostAddress();
  206.     }
  207.  
  208.     /**
  209.      * Returns the translated path for the specified virtual path.
  210.      */
  211.     public String getRealPath(String path) {
  212.     return server.getRealPath(path);
  213.     }
  214.  
  215.     /**
  216.      * Returns the input stream for reading from the connection.
  217.      */
  218.     public InputStream getInputStream() throws IOException {
  219.     return socket.getInputStream();
  220.     }
  221.  
  222.     /**
  223.      * Returns the output stream for writing to the connection.
  224.      */
  225.     public OutputStream getOutputStream() throws IOException {
  226.     return socket.getOutputStream();
  227.     }
  228. }
  229.